import 'package:flutter_test/flutter_test.dart';
import 'package:sow_app/repository/ajax_rest_client.dart';
import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';

void main() async {
  group('token repository test group', () {
    AjaxRestClient client = AjaxRestClient();

    test(
      "should have same authorization value when invoke setAuthorization()",
      () async {
        // arrange
        // act
        client.setAuthorization("666");
        // assert
        expect(client.authorization, '666');
      },
    );
    test(
      "should have same HostEnv value when invoke setHostEnv()",
      () async {
        // arrange
        // act
        client.setHostEnv(I18n.backend_ip, I18n.backend_port);
        // assert
        expect(client.host, I18n.backend_ip);
        expect(client.port, I18n.backend_port);
      },
    );
    test(
      "should return not empty file uint8list when invoke getFile()",
      () async {
        // arrange
        // act
        final file = await client.getFile('/file/getVerify');
        // assert
        expect(file, isNotEmpty);
      },
    );
    test(
      "should return valid response when invoke invokeHttp()",
      () async {
        // arrange
        // act
        const novelId = '1262260513468559361';
        final response = await client.invokeHttp(
            'book/queryBookDetail/$novelId', RequestType.get);
        // assert
        expect(response.data, isNotEmpty);
      },
    );
    test(
      "should return ServerException when invoke invokeHttp() with wrong param",
      () async {
        // arrange
        const novelId = '1262260513468559000';
        // act
        try {
          await client.invokeHttp(
              'book/queryBookDetail/$novelId', RequestType.get);
          // assert
        } catch (e) {
          expect(e, isA<ServerException>());
        }
      },
    );
    test(
      "should return Exception when invoke invokeHttp() with invalid body",
      () async {
        // arrange
        const novelId = '1262260513468559000';
        // act
        try {
          await client.invokeHttp(
              'book/queryBookDetail/$novelId', body: 123, RequestType.get);
          // assert
        } catch (e) {
          expect(e, isA<Exception>());
        }
      },
    );
  });
}
